package com.harmonyos.service.impl;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.harmonyos.domain.Limit;
import com.harmonyos.domain.TbNews;
import com.harmonyos.mapper.TbNewsMapper;
import com.harmonyos.service.TbNewsService;
import com.harmonyos.swagger.RedisUtil;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * TODO
 *
 * @Description
 * @Author lanmo
 * @Date 2023/1/13 16:49
 **/
@Service
public class TbNewsServiceImpl implements TbNewsService {
    @Resource
    private TbNewsMapper tbNewsMapper;
    @Resource
    private RedisUtil redisUtil;
    private final String key="news:";

    @Override
    public String installNews(TbNews news) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            tbNewsMapper.insert(news);
            redisUtil.set(key+news.getId(),(Object)news,60*60);
            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String deleteNews(int id) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            tbNewsMapper.deleteById(id);
            objectObjectHashMap.put("code","200");
            if (redisUtil.hasKey(key+id)) {
               redisUtil.del(key+id);
            }
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String updateNews(TbNews news) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            tbNewsMapper.updateById(news);
            objectObjectHashMap.put("code","200");
            redisUtil.set(key+news.getId(), (Object)news,60*60);
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String selectlimit(String json) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            Limit<TbNews> limit = JSONUtil.toBean(json, Limit.class);
            PageHelper.startPage(limit.getCurrent(),limit.getSize());
            List<TbNews> selectlimit = tbNewsMapper.selectlimit(limit.getKey());
            PageInfo<TbNews> info = new PageInfo<>(selectlimit);
            List<TbNews> tbNews=info.getList();
            objectObjectHashMap.put("data",tbNews);
            objectObjectHashMap.put("code","200");
            objectObjectHashMap.put("total",info.getTotal());
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String selectAllNews() {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            List<TbNews> tbNews=new ArrayList<>();
            Set<String> keys = redisUtil.keys(key);
            if (keys.size() != 0) {
                for (String s : keys) {
                    tbNews.add((TbNews) redisUtil.get(s));
                }

            }else {
                tbNews.addAll(tbNewsMapper.selectList(null));
                tbNews.forEach(tbNew -> {
                    redisUtil.set(key+tbNew.getId(), (Object)tbNew,60*60);
                });
            }
            objectObjectHashMap.put("data",tbNews);
            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }
}
